//
//  LHQDestinationDetailPhotoFilmViewController.m
//  God_is_a_Girl
//
//  Created by dllo on 15/9/25.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "LHQDestinationDetailPhotoFilmViewController.h"
#import "LHQDestinationDetailPhotoFilmViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface LHQDestinationDetailPhotoFilmViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) NSMutableArray   *arr;

@end

@implementation LHQDestinationDetailPhotoFilmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  dataHandel];
    [self  collectionViewSubView];

}
-(void)dataHandel
{
    self.imageArr = [NSMutableArray  array];
    
    self.textArr = [NSMutableArray  array];
    
    self.photo_infoArr = [NSMutableArray  array];
    
    self.timeArr = [NSMutableArray array];
    
    
    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManager   manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet  setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];//支持所有接口类型
    
    NSString *str = [NSString  stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/photos/?start=0&count=21&gallery_mode=true",self.type,self.idStr];
    
    [manager  GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        //解析items数据
        
        NSArray *itemsArr = dic[@"items"];
        for (NSDictionary *tempDic in itemsArr) {
            
            //照片的url数组
            [self.imageArr addObject:tempDic[@"photo"]];
            
            //照片尺寸数
            [self.photo_infoArr  addObject:tempDic[@"photo_info"]];
            [self.textArr  addObject:tempDic[@"text"]];
            
            //时间数组
            [self.timeArr  addObject:tempDic[@"local_time"]];
            
            UICollectionView  *collectionView = (UICollectionView *)[self.view  viewWithTag:1000];
            
            [collectionView reloadData];
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
-(void)collectionViewSubView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT-49- 64);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
    
    
    collectionView.tag = 1000;
    [collectionView registerClass:[LHQDestinationDetailPhotoFilmViewCell class] forCellWithReuseIdentifier:@"reuse"];
    collectionView.pagingEnabled = YES;
    
    collectionView.contentOffset=CGPointMake(WIDTH*self.index, 0);
    
}
//collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LHQDestinationDetailPhotoFilmViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    //    设置图片的自适应以及居中放置
    NSDictionary *dic = self.photo_infoArr[indexPath.row];
    double imageHeight = WIDTH * [dic[@"h"] doubleValue] / [dic[@"w"] doubleValue];
    cell.myImageView.frame = CGRectMake(0, 0, WIDTH, imageHeight);
    cell.myImageView.center = cell.contentView.center;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
    
    //  textLabel自适应
    cell.textlabel.text = self.textArr[indexPath.row];
    NSDictionary *Dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    
    CGRect rect = [cell.textlabel.text boundingRectWithSize:CGSizeMake(cell.contentView.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:Dic context:nil];
    cell.textlabel.frame = CGRectMake(10, 0, cell.contentView.frame.size.width, rect.size.height);
    cell.myView.frame = CGRectMake(0, cell.contentView.frame.size.height - 30 - cell.textlabel.frame.size.height, cell.contentView.frame.size.width, cell.textlabel.frame.size.height + 30);
    cell.timeLabel.frame = CGRectMake(10, cell.myView.frame.size.height - 30, 150, 30);
    cell.timeLabel.text = self.timeArr[indexPath.row];
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
